# Maintainer: Rafał Brzegowy <rafal.brzegowy@yahoo.com>

_realname=frontistr
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=5.5
pkgrel=2
pkgdesc='Open-Source Large-Scale Parallel FEM Program for Nonlinear Structural Analysis (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-parmetis"
         "${MINGW_PACKAGE_PREFIX}-metis"
         "${MINGW_PACKAGE_PREFIX}-msmpi"
         # "${MINGW_PACKAGE_PREFIX}-mumps" Couldn't be found by cmake!
         "${MINGW_PACKAGE_PREFIX}-netcdf"
         "${MINGW_PACKAGE_PREFIX}-seacas"
         "${MINGW_PACKAGE_PREFIX}-trilinos"
         "${MINGW_PACKAGE_PREFIX}-doxygen"
         "${MINGW_PACKAGE_PREFIX}-scalapack"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"))
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
license=('MIT')
url="https://www.frontistr.com"
source=("https://github.com/${_realname}/${_realname}/archive/v${pkgver}.tar.gz")
sha256sums=('d4fb213d7b57ad84dca96949c6ece0d8a4499e4a2dd2deb2d038a2e605bf6005')

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    export FFLAGS="-fallow-invalid-boz"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      ${_extra_config[@]} \
      -DWITH_MPI=ON \
      -DWITH_ML=ON \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
  
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR=${pkgdir} cmake --install .
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/License.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE 

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in $(find "${pkgdir}${MINGW_PREFIX}"/lib/cmake -type f); do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done
  
}
